package client;

import java.io.IOException;
import java.net.ServerSocket;
import java.security.InvalidKeyException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;

import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import utils.ConstantVariables;

/**
 * Bob has one client thread communicating with Mallory.
 * 
 * Bob listens to 127.0.0.1:55555
 * 
 * @author bh349
 * 
 */
public class Bob extends Client
{

    public Bob() throws InvalidKeySpecException, NoSuchAlgorithmException,
            IOException {
        super();
        serverSocket = new ServerSocket( ConstantVariables.BOB_PORT );
        new ClientThread( this ).start();
    }

    /**
     * @param args
     * @throws IOException
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeySpecException
     * @throws IllegalBlockSizeException
     * @throws NoSuchPaddingException
     * @throws InvalidKeyException
     */
    public static void main( String[] args ) throws InvalidKeySpecException,
            NoSuchAlgorithmException, IOException, InvalidKeyException,
            NoSuchPaddingException, IllegalBlockSizeException {
        System.out.println( "This is Bob." );
        Bob bob = new Bob();
        bob.run();
    }

    private void run() throws IOException, InvalidKeySpecException,
            NoSuchAlgorithmException, InvalidKeyException,
            NoSuchPaddingException, IllegalBlockSizeException {
        dstPublicKey = ConstantVariables.getPublicKeyFromFile( "Alice" );
        publicKey = ConstantVariables.getPublicKeyFromFile( "Bob" );
        createMsgs( this );
    }

    @Override
    protected PrivateKey getPrivateKeyFromFile()
            throws InvalidKeySpecException, NoSuchAlgorithmException,
            IOException {
        byte[] encodedKeyBytes;
        encodedKeyBytes = ConstantVariables
                .getBytesFromFile( ConstantVariables.BOB_PRIVATEKEY );
        return KeyFactory.getInstance( "RSA" ).generatePrivate(
                new PKCS8EncodedKeySpec( encodedKeyBytes ) );
    }
}